package com.plant.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plant.entity.PgcHottestFaq;
import com.plant.entity.PgcMonthList;
import com.plant.entity.PgcWeekList;
import com.plant.entity.UgcUser;
import com.plant.service.FuzzySearchService;
import com.plant.service.HelpService;
import com.plant.tool.CalculateDate;
import com.plant.tool.DateUtils;
import com.plant.tool.ListMapSort;


@Controller
@RequestMapping(value = "/faq")
public class HotFaqAnswerContraller {
	@Resource(name="helpServiceImpl")
	private HelpService helpService;
	@Resource(name = "fuzzySearchServiceImpl")
	private FuzzySearchService fuzzySearchService;

	@RequestMapping(value = "hot_faq")//最热问答
	@ResponseBody
	public Map<String, Object> FaqAndAnswer(Integer page,PgcHottestFaq hotfaq,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				if(page<=2){
				List<Map<String, Object>> allfaq=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> allfaqs=new ArrayList<Map<String,Object>>();
				allfaq=fuzzySearchService.HotFaq(page,hotfaq);
				allfaqs=fuzzySearchService.HotFaq(page+1,hotfaq);
				if(allfaqs.size()>0){
					map.put("hasmore", 1);
				}else{
					map.put("hasmore", 0);
				}
				for (Map<String, Object> map2 : allfaq) {
					String temptime=map2.get("faq_create_time").toString();
					String time=DateUtils.fullformatDate(DateUtils.StaticgetUnixStamps(temptime));
					map2.put("create_time", time);
				}
				ListMapSort.Sort(allfaq);
				map.put("allfaq", allfaq);
				map.put("message", "请求成功");
				map.put("state", "1");
				}else{
					map.put("message", "只用展示20条排名");
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}
	
	// 用于测试插入最热问题
	@RequestMapping(value = "test_hot_faq")
	@ResponseBody
	public Map<String, Object> TFaqAndAnswer(PgcHottestFaq hotfaq,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Map<String, Object>> allfaq=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> allfaqinf=new ArrayList<Map<String,Object>>();
				List<Object> faqids=new ArrayList<Object>();
				allfaq=fuzzySearchService.ListAllFaqId();
				for (Map<String, Object> map2 : allfaq) {
					Object faqid=map2.get("id_key");
					faqids=fuzzySearchService.AllAnswerIds(faqid);
					if(faqids.size()==0){
						map2.put("count_like", 0);
						map2.put("answer_count", helpService.OneFaqAnswerCount(faqid));
					}else{
						Object aids=StringUtils.join(faqids.toArray(),",");
						map2.put("count_like", fuzzySearchService.CountAllFaqAnswerCount(aids));
						map2.put("answer_count", helpService.OneFaqAnswerCount(faqid));
					}
				}
				ListMapSort.Sort(allfaq);
				allfaqinf=ListMapSort.ListSomeInf(allfaq);
				Date date=new Date();
				Timestamp time=new Timestamp(date.getTime());//获取当前时间
				Integer count=0;
				for (Map<String, Object> map2 : allfaqinf) {
					Integer user_id=Integer.parseInt(map2.get("user_id").toString());
					Integer faq_id=Integer.parseInt(map2.get("id_key").toString());
					Integer answer_count=Integer.parseInt(map2.get("answer_count").toString());
					Integer count_like=Integer.parseInt(map2.get("count_like").toString());
					String title=map2.get("title").toString();
					Timestamp faqtime=(Timestamp) map2.get("create_time");
					count=count+1;
					hotfaq.setSort(count);
					hotfaq.setCreateTime(time);
					hotfaq.setFaqAnswerCount(answer_count);
					hotfaq.setFaqAnswerLike(count_like);
					hotfaq.setFaqTitle(title);
					hotfaq.setFaqId(faq_id);
					hotfaq.setUserId(user_id);
					hotfaq.setFaqCreateTime(faqtime);
					fuzzySearchService.AddhotFaq(hotfaq);
				}
				map.put("message", "请求成功");
				map.put("state", "1");
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}

	
	//用于 插入周排行用户
	@RequestMapping(value = "hot_people")//周排行用户
	@ResponseBody
	public Map<String, Object> HotPeople(UgcUser u,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				PgcWeekList week=new PgcWeekList();
				Date date=new Date();
				String weekfirstday=CalculateDate.getTimefirstday(date);
				String weeklastday=CalculateDate.getTimelastDay(date);
				List<Map<String, Object>> faq=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> temp=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> threeinf=new ArrayList<Map<String,Object>>();
				Set<Integer> set=new HashSet<Integer>();
				faq=fuzzySearchService.HelpedPeople(weekfirstday,weeklastday);
				for (Map<String, Object> map2 : faq) {
					Integer user_id=Integer.parseInt(map2.get("user_id").toString());
					set.add(user_id);
				}
				Iterator<Integer> user_id=set.iterator();
				while (user_id.hasNext()) {
					Map<String,Object> map1=new HashMap<String, Object>();
					Integer integer = (Integer) user_id.next();
					u.setId(integer);
					map1.put("user_id", integer);
					map1.put("helped_people_count", fuzzySearchService.HelpedCount(u));
					map1.put("count_like", fuzzySearchService.OneUserAnswerBylikeCount(u));
					temp.add(map1);
				}
				ListMapSort.Sort(temp);
				if(temp.size()>2){//取三条
					threeinf=ListMapSort.ListThreeInf(temp);
					Timestamp time=new Timestamp(date.getTime());
					for (Map<String, Object> map2 : threeinf) {
						Integer uid=Integer.parseInt(map2.get("user_id").toString());
						Integer faq_like_count=Integer.parseInt(map2.get("count_like").toString());
						Integer helpcount=Integer.parseInt(map2.get("helped_people_count").toString());
						week.setUserId(uid);
						week.setFaqLikeCount(faq_like_count);
						week.setHelpedPeopleCount(helpcount);
						week.setCreateTime(time);
						fuzzySearchService.AddHotWeekList(week);
					}
					map.put("threeinf", threeinf);
				}
				map.put("message", "请求成功");
				map.put("state", "1");
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}

	
	//用于 插入月排行用户
	@RequestMapping(value = "hot_month_people")//月排行用户
	@ResponseBody
	public Map<String, Object> HotMonthPeople(UgcUser u,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				PgcMonthList pm=new PgcMonthList();
				Date date=new Date();
				String weekfirstday=CalculateDate.getmonthfirst();
				String weeklastday=CalculateDate.getmonthlasttime();
				List<Map<String, Object>> faq=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> temp=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> threeinf=new ArrayList<Map<String,Object>>();
				Set<Integer> set=new HashSet<Integer>();
				faq=fuzzySearchService.HelpedPeople(weekfirstday,weeklastday);
				for (Map<String, Object> map2 : faq) {
					Integer user_id=Integer.parseInt(map2.get("user_id").toString());
					set.add(user_id);
				}
				Iterator<Integer> user_id=set.iterator();
				while (user_id.hasNext()) {
					Map<String,Object> map1=new HashMap<String, Object>();
					Integer integer = (Integer) user_id.next();
					u.setId(integer);
					map1.put("user_id", integer);
					map1.put("helped_people_count", fuzzySearchService.HelpedCount(u));
					map1.put("count_like", fuzzySearchService.OneUserAnswerBylikeCount(u));
					temp.add(map1);
				}
				ListMapSort.Sort(temp);
				if(temp.size()>2){//取三条
					threeinf=ListMapSort.ListThreeInf(temp);
					Timestamp time=new Timestamp(date.getTime());
					for (Map<String, Object> map2 : threeinf) {
						Integer uid=Integer.parseInt(map2.get("user_id").toString());
						Integer faq_like_count=Integer.parseInt(map2.get("count_like").toString());
						Integer helpcount=Integer.parseInt(map2.get("helped_people_count").toString());
						pm.setUserId(uid);
						pm.setFaqLikeCount(faq_like_count);
						pm.setHelpedPeopleCount(helpcount);
						pm.setCreateTime(time);
						fuzzySearchService.AddHotMonthList(pm);
					}
					map.put("threeinf", threeinf);
				}
				map.put("message", "请求成功");
				map.put("state", "1");
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}


}
